<?php
class Web_Model_AuthCode
{
    /**
     * 生成并输出验证码
     */
    public static function show()
    {
        Header("Content-type: image/gif");
        $code = '';
        $width = 75;
        $height = 24;
        srand((double)microtime()*1000000); 
        $im = ImageCreate($width, $height);
        $bgcolor = ImageColorAllocate($im, 200, 233, 252);
        ImageFill($im, 0, 0, $bgcolor);
        $textColor = ImageColorAllocate($im, 0,0,0);
        for($i = 0;$i<6;$i++){
            $num = mt_rand(0, 9);
            $code .= $num;
            ImageChar($im, 5, $i*10+8, 4, $num, $textColor);
        }
        Imagegif($im);
        ImageDestroy($im);
        Eren_Registry::get('SESSION')->set('code', $code);
    }
    
    /**
     * 校验验证码
     * 
     * @return bool
     */
    public static function veify($uCode)
    {
        $code = Eren_Registry::get('SESSION')->get('code');
        Eren_Registry::get('SESSION')->set('code', '');
        if($code == '' || $uCode != $code){
            return FALSE;
        }
        return TRUE;
    }
}
